
#include "arrow.h"
#include "resourcemanager.h"
#include "gamemanager.h"

#define __super         Bullet

static const float ARROW_BULLET_SPEED = 600.0f;

ArrowBullet::ArrowBullet(const CCPoint& from, const CCPoint& to, EquipmentAttribute* attr) :
__super(attr), velocity(ccp(ARROW_BULLET_SPEED, 0)) {
	src = from;
	dst = to;
	position = src;

	if(src.x > dst.x) {
		velocity.x = -velocity.x;
	}

	velocity.y = 0.5f * GRAVITY * static_cast<float>(fabs(dst.x - src.x)) / static_cast<float>(fabs(velocity.x));
}

ArrowBullet::~ArrowBullet() {
}

void ArrowBullet::init() {
	__super::init();

	sprite = MAKE_SPRITE("weapon/arrow.png");
	sprite->setZOrder(999);
	sprite->setPosition(position);
	sprite->setScale(GM->minscale * 0.45f);
}

bool ArrowBullet::update(float ticks) {
	bool ret = true;

	position.x += velocity.x * ticks;
	position.y += velocity.y * ticks;
	velocity.y -= GRAVITY * ticks;

	sprite->setPosition(position);
	float rotation = -180.0f * ccpToAngle(velocity) / static_cast<float>(M_PI) + 90;
	sprite->setRotation(rotation);

	if((src.x <= dst.x && position.x >= dst.x) || (src.x >= dst.x && position.x <= dst.x)) {
		ret = false;
	}

	return ret;
}
